﻿using MediatR;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Read.API.AppLication.Command.SaleReturnCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.SaleReturnCommandHandler
{
    public class SaleReturnsShowCommandHandler : IRequestHandler<SaleReturnsShowCommand, List<SalesReturnDTO>>
    {
        private readonly ISaleReturnsReposiotry saleReturnsReposiotry;

        private readonly ISalesOrdersRepository salesOrdersRepository;

        public SaleReturnsShowCommandHandler(ISaleReturnsReposiotry saleReturnsReposiotry, ISalesOrdersRepository salesOrdersRepository)
        {
            this.saleReturnsReposiotry = saleReturnsReposiotry;
            this.salesOrdersRepository = salesOrdersRepository;
        }

        async Task<List<SalesReturnDTO>> IRequestHandler<SaleReturnsShowCommand, List<SalesReturnDTO>>.Handle(SaleReturnsShowCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await salesOrdersRepository.GetAllAsaync()
                       join b in await saleReturnsReposiotry.GetAllAsaync() on a.SalesOrdersNo equals b.SalesOrdersNo
                       where a.IsDelete == false
                       select new SalesReturnDTO
                       {
                           SalesOrdersNo = a.SalesOrdersNo,
                           ProcurementTypeId = a.ProcurementTypeId,
                           AssociateNumber = a.AssociateNumber,
                           ClientNo = a.ClientNo,
                           ClientName = a.ClientName,
                           SalesOrdersPerson = a.SalesOrdersPerson,
                           SalesOrdersPhone = a.SalesOrdersPhone,
                           ShipmentsDate = a.ShipmentsDate,
                           SalesOrdersRemark = a.SalesOrdersRemark,
                           SingleMaker = a.SingleMaker,
                           SingleTime = a.SingleTime,
                           OrderStatus = a.OrderStatus,
                           OrderSale = a.OrderSale,
                           Recorded = a.Recorded,
                           office_id = a.office_id,
                           user_id = a.user_id,
                           SaleReturnsId = b.SaleReturnsId,
                           ProductName = b.ProductName,
                           ProductNo = b.ProductNo,
                           ProductSpec = b.ProductSpec,
                           UnitName = b.UnitName,
                           Price = b.Price,
                           SaleReturnsNo = b.SaleReturnsNo,
                           SaleReturnsNum = b.SaleReturnsNum,
                           SaleReturnsTotal = b.SaleReturnsTotal,
                           SaleReturnsState=b.SaleReturnsState,
                           CreateName = b.CreateName,
                           CreateTime= b.CreateTime,
                           UpdateName= b.UpdateName,
                           UpdateTime= b.UpdateTime,
                           DeleteName= b.DeleteName,
                           DeleteTime= b.DeleteTime,
                           IsDelete= b.IsDelete,
                       };
            if (request.SalesOrdersNo != "")
            {
                list = list.Where(w => w.SalesOrdersNo == request.SalesOrdersNo).ToList();
            }
            if (request.ProductName != "")
            {
                list = list.Where(w => w.ProductName.Contains(request.ProductName)).ToList();
            }
            if (request.SaleReturnsNo != "")
            {
                list = list.Where(w => w.SaleReturnsNo == request.SaleReturnsNo).ToList();
            }
            if (request.OrderStatus != 0)
            {
                list = list.Where(w => w.OrderStatus == request.OrderStatus).ToList();
            }
            if (request.ProductNo != "")
            {
                list = list.Where(w => w.ProductNo == request.ProductNo).ToList();
            }
            if (request.ClientNo != "")
            {
                list = list.Where(w => w.ClientNo==request.ClientNo).ToList();
            }
            if (request.ClientName != "")
            {
                list = list.Where(w => w.ClientName == request.ClientName).ToList();
            }
            return list.ToList();
        }
    }
}
